package com.example.muyonghui.mytimetable;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddCourseByHandActivity extends AppCompatActivity {

    private EditText courseName;

    private EditText courseTeacher;

    private EditText courseTime;

    private EditText courseAddress;

    private Button save;

    private MyDatabaseHelper dbHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addcourse_byhand);

        dbHelper = new MyDatabaseHelper(this, "UserStore.db", null, 3);

        courseName = (EditText) findViewById(R.id.add_course_name);
        courseTeacher = (EditText) findViewById(R.id.add_course_teacher);
        courseTime = (EditText) findViewById(R.id.add_course_time);
        courseAddress = (EditText) findViewById(R.id.add_course_address);
        save = (Button) findViewById(R.id.save);


        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String courseNametxt = courseName.getText().toString();
                String courseTeachertxt = courseTeacher.getText().toString();
                String courseTimetxt = courseTime.getText().toString();
                String courseAddresstxt = courseAddress.getText().toString();
                if (TextUtils.isEmpty(courseNametxt)) {
                    Toast.makeText(AddCourseByHandActivity.this, "联系人姓名不能为空",
                            Toast.LENGTH_SHORT).show();
                }else{
                    save(courseNametxt, courseTeachertxt, courseTimetxt, courseAddresstxt);

                    Toast.makeText(AddCourseByHandActivity.this, "已存储",
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(AddCourseByHandActivity.this, MainActivity.class);
                    AddCourseByHandActivity.this.startActivity(intent);
                    finish();
                }
            }
        });



    }
    public void save(String courseNametxt,String courseTeachertxt,String courseTimetxt,String courseAddresstxt){


        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        String username = pref.getString("username", "");
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        // 开始组装第一条数据
        values.put("username", username);
        values.put("course_name", courseNametxt);
        values.put("course_teacher", courseTeachertxt);
        values.put("course_time", courseTimetxt);
        values.put("course_address", courseAddresstxt);
        db.insert("course", null, values); // 插入数据
        values.clear();
    }


}
